package com.xxxy.dongyexuan.action;
import com.xxxy.dongyexuan.mapper.ILevelMapper;
import com.xxxy.dongyexuan.mapper.IProjectMapper;
import com.xxxy.dongyexuan.mapper.IWorkMapper;
import com.xxxy.dongyexuan.po.Level;
import com.xxxy.dongyexuan.po.PageBean;
import com.xxxy.dongyexuan.po.Project;
import com.xxxy.dongyexuan.po.Work;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class WorkAction {

    @Autowired
    private IWorkMapper mapper;


    //分页查询
    @GetMapping("/work/{pagenow}/{pagesize}")
    public PageBean findByPage(@PathVariable("pagenow") int pagenow, @PathVariable("pagesize") int pagesize){
        int from =(pagenow-1)*pagesize;//开始条数
        int total = this.mapper.getcount();//总条数
        int pages =(total-1)/pagesize+1;//计算总页数
        List data = this.mapper.findByPage(from,pagesize);
        PageBean pageBean = new PageBean();
        pageBean.setPages(pages);
        pageBean.setPagenow(pagenow);
        pageBean.setPagesize(pagesize);
        pageBean.setTotal(total);
        pageBean.setData(data);
        return pageBean;
    }



    //查询所有
    @GetMapping("/work")
    public List<Work> findAll(){
        List<Work> list = this.mapper.findAll();
        return list;
    }

    //根据员工号查询
    @GetMapping("/work/{wid}")
    public Work findById(@PathVariable("wid") int wid){
        Work work = this.mapper.findById(wid);
        return work;
    }

    //增加
    @PostMapping("/work")
    public int save(Work work){
        int result = this.mapper.save(work);
        return result;
    }

    //修改
    @PutMapping("/work")
    public int update(Work work){
        int result = this.mapper.merge(work);
        return result;
    }

    //删除
    @DeleteMapping("/work/{wid}")
    public int delete(@PathVariable("wid") int wid){
        int result = this.mapper.delete(wid);
        return result;
    }
}